<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="rules" @submit.native.prevent="handleLogin">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username" autocomplete="username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="loginForm.password" autocomplete="current-password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
      </el-form-item>
      <el-form-item>
        <div class="register-link">
          没有账号？<el-button type="text" @click="handleGoToRegister">注册一个</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { max: 15, message: '密码长度不能超过15位', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 执行登录逻辑
          console.log('登录成功', this.loginForm);
          // 通常会在这里发送请求到后端进行验证
        } else {
          console.log('输入有误！');
          return false;
        }
      });
    },
    handleGoToRegister() {
      // 跳转到注册页面的逻辑
      this.$router.push('/register');
    }
  }
};
</script>

<style scoped>
.login-container {
  width: 300px;
  margin: 50px auto;
  padding: 20px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.register-link {
  text-align: center;
}
</style>
